package org.onlineCode.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.onlineCode.dao.LanguageDao;
import org.onlineCode.dao.UserDao;
import org.onlineCode.pojo.Language;
import org.onlineCode.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserDao userDao;
	@Resource
	private LanguageDao languageDao;

	public void setLanguageDao(LanguageDao languageDao) {
		this.languageDao = languageDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username, String password, HttpSession session) throws Exception {
		User user = userDao.login(username, password);
		if (user != null) {
			session.setAttribute("login_user", user);
			return "redirect:/user/main";
		}
		session.setAttribute("error", "用户名或者密码错误");
		return "redirect:/login.jsp";
	}
	
	@RequestMapping(value="/regist",method=RequestMethod.POST)
	public String regist(User user) throws Exception {
		userDao.add(user);
		return "redirect:/login.jsp";
	}
	
	@RequestMapping("/main")
	public String main(ModelMap modelMap) throws Exception {
		List<Language> list = languageDao.getAll();
		modelMap.put("languages", list);
		return "main";
	}
	
}







